package net.xinqushi.common;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Consumer;

/**
 * @author xjie 2019/7/29 11:27
 * @description 泛型测试
 */
public class TypeReferenceTest {

    public static void main(String[] args) {
        List<Element> list = new ArrayList<>();
        Collection<SunElement> sunCollection = new ArrayList<>();
        list.addAll(sunCollection);// ? extends
        list.forEach(new Consumer<SuperElement>() { // ? super
            public void accept(SuperElement element) {

            }
        });

        SuperDefaultQueue<Element> queue = new SuperDefaultQueue();
        ExtendsDefaultQueue<Element> queue2 = new ExtendsDefaultQueue();
        queue2.removeLast(new Element<SuperElement>());
        queue2.removeLast(new Element<Element>());
        queue.addFirst(new Element<Element>());
        queue.addFirst(new Element<SunElement>());
    }

    interface MyQueue<T>{
        void addFirst(Element<? extends T> c);
        void removeLast(Element<? super T> c);
    }

    static abstract class DefaultQueue<T> implements MyQueue<T>{
        public void addFirst(Element<? extends T> c){}
        public void removeLast(Element<? super T> c){}
    }

    //Test  (super ? ele)     and     ( ele ? extends)
    static class SuperDefaultQueue<T> extends DefaultQueue<T>{
        public void addFirst(Element<? extends T> c) {
        }
    }

    static class ExtendsDefaultQueue<T> extends DefaultQueue<T>{
        public void removeLast(Element<? super T> c) {
        }
    }


    //ele
    interface SuperElement<T>{
    }
    static class Element<T> implements SuperElement<T>{
    }
    static class  SunElement<T> extends Element<T>{}


}
